var TextListMap = new Object();
var TextList = function(opt){
	this.target = opt.target?opt.target:'textList';
	this.content = opt.content?opt.content:'pTextListBox';
	this.disNum = opt.disNum?opt.disNum:9;				//导航显示个数
	this.itemHeight = opt.itemHeight?opt.itemHeight:54;	//分类高度
	this.focusClass = opt.focusClass?opt.focusClass:'_textListCurItem';
	this.normalClass = opt.normalClass?opt.normalClass:'_textListItem';
	this.emptyClass = opt.emptyClass?opt.emptyClass:'_textListItem0';
	this.tipClass = opt.tipClass?opt.tipClass:'_textListTip';

	this.onfocus = opt.onfocus?opt.onfocus:false;		//焦点在导航上
	this.onblur = opt.onblur?opt.onblur:false;		//焦点在item上
	this.onclick = opt.onclick?opt.onclick:false;		//点击事件
	
	if(opt.formatHtml){
		this.formatHtml = opt.formatHtml;
	}
	
	this.curItemId = 0;
	this.focusItemId = 0;
	this.topItemId = 0;
	this.dataList = new Array();
	this.showSplit = false;
	this.splitHeight = opt.splitHeight?opt.splitHeight:486;
	this.splitBar = opt.splitBar?opt.splitBar:'pTextListSplitBar';
	this.splitAddValue = 0;
	this.splitBarHeight = 0;
	this.tip=opt.tip?opt.tip:'';
	TextListMap[this.target] = this;
}

TextList.prototype = {
	getFocusData:function(){
		if(this.dataList && this.dataList.length > 0){
			return this.dataList[this.focusItemId - 1];
		}
		return false;
	},
	getData:function(){
		if(this.dataList && this.dataList.length > 0){
			return this.dataList[this.focusItemId - 1];
		}
		return false;
	},
	setFocus:function(id){
		var a = false;
		if(id){
			a = document.getElementById(this.target+'-a-'+id);
		}else{
			a = document.getElementById(this.target+'-a-'+this.focusItemId);
		}
		if(a){
			a.focus();
		}
	},
	appendDataList:function(list){
		if(list && list.length > 0){
			for(var i = 0;i<list.length;i++){
				this.dataList.push(list[i]);
			}
			this.format();
			
			var listBox = document.getElementById(this.content);
			var childNodes = listBox.childNodes;
			
			listBox.removeChild(childNodes[0]);		//删除第一个节点
			listBox.removeChild(childNodes[childNodes.length - 1]);		//删除最后一个节点
			
			var nId = this.focusItemId + 1;
			this.appendDiv(nId,listBox);
			this.appendDiv(nId+1,listBox);
			this.topItemId++;
			this.showSplitBox();
		}
	},
	isLast:function(){
		return this.focusItemId == this.dataList.length;
	},
	clear:function(){
		var box = document.getElementById(this.content);
		box.innerHTML = '';
		var splitBar = document.getElementById(this.splitBar); 
		var par = splitBar.parentNode;
		par.style.display = 'none';
	},
	setType:function(type){
		this.dataList = type.dataList;
		this.topItemId = type.topItemId;
		this.focusItemId = type.focusItemId;
		this.curItemId = type.curItemId;
		this.showSplit = type.showSplit;
		this.splitAddValue = type.splitAddValue;
		this.splitBarHeight = type.splitBarHeight;
		
		this.refreshContent();
		
	},
	setDataList:function(list,onfocus){if(!list){list=new Array();}
		this.dataList = list;
		
		this.format();
		this.focusItemId = 1;
		this.curItemId = 1;
		this.topItemId = 0;
		
		this.refreshContent();
		
		if(onfocus){
			var a = document.getElementById(this.target+'-a-'+this.focusItemId);
			if(a){
				a.focus();
			}
		}
	},
	formatType:function(type){
		var length = type.dataList.length
		if(length > this.disNum){
			type.showSplit = true;
			if(length< 2*this.disNum){
				type.splitAddValue = this.itemHeight;
			}else{
				type.splitAddValue = (this.splitHeight - this.itemHeight)/(length - this.disNum);
			};
			type.splitBarHeight = this.splitHeight - (length - this.disNum)*type.splitAddValue;
		}else{
			type.showSplit = false;
		}
		type.focusItemId = 1;
		type.curItemId = 1;
		type.topItemId = 0;
		
		return type;
	},
	format:function(){
		var length = this.dataList.length;
		if(length > this.disNum){
			this.showSplit = true;
			/*
			if(length< 2*this.disNum){
				this.splitAddValue = this.itemHeight;
			}else{
				this.splitAddValue = (this.splitHeight - this.itemHeight)/(length - this.disNum);
			}
			this.splitBarHeight = this.splitHeight - (length - this.disNum)*this.splitAddValue;
			*/
			this.splitAddValue = (this.splitHeight - 50)/(length  - this.disNum);
		}else{
			this.showSplit = false;
		}
		
	},
	refreshContent:function(){
		var dataList = this.dataList;
		var start = this.topItemId;
		var end = start + this.disNum + 2;
		var length = dataList.length; 
		
		var j,data;
		var con = ''; 
		if(dataList.length > 0){
			for(var i = start;i<end;i++){
				j = i;
				
				if(j<1 || j>length){
					con+='<div class="'+this.emptyClass+'">';
					con+='&nbsp;';
				}else{
					con+='<div class="'+this.normalClass+'">';
					data =dataList[j-1];
					con+=this.formatHtml(data,j);
				}
				con+='</div>';
			}
		}else{
			con='<div class="'+this.tipClass+'">' + this.tip + '</div>';;
			
		}
		
		var box = document.getElementById(this.content);
		box.innerHTML = con;
		
		this.showSplitBox();
		var aList = box.getElementsByTagName('a');
		var a;
		for(var i = 0;i<aList.length;i++){
			a = aList[i];
			a.onfocus = this.focusItem;
			a.onblur = this.blurItem;
			a.onclick = this.clickItem;
		}
	},
	formatHtml:function(data,id){
		var con = '';
		con += '<div class="_textListNum">'+id+'</div>';
		con += '<div class="_textListText"><a href="#" id="'+this.target+'-a-'+id+'">'+data.name+'</a></div>';
		return con;
	},
	showSplitBox:function(){
		var splitBar = document.getElementById(this.splitBar); 
		var par = splitBar.parentNode;
		if(this.showSplit){
			par.style.display ='block';
			//splitBar.style.height =this.splitBarHeight+'px';
			this.changeSplit();
			return true;
		}else{
			par.style.display ='none';	
			return false;
		}
	},
	changeSplit:function(){
		if (this.showSplit) {
			var topItemId = this.topItemId;
			var max = this.dataList.length - this.disNum;
			if(topItemId>max){
				topItemId = max;
			}
					
			var top = topItemId * this.splitAddValue;
			var splitBar = document.getElementById(this.splitBar);
			splitBar.style.top = top+'px';
		}
	},
	focusItem:function(event){
		var a = event.currentTarget;
		var par = a.parentNode.parentNode;
		var id = a.id;
		var idArray = id.split('-');
		var _this = TextListMap[idArray[0]];
		var tar = parseInt(idArray[2]);
		
		par.className = _this.focusClass; 
		if(_this.showSplit){
			_this.moveItem(tar);
		}else{
			_this.focusItemId = tar;
		}
		if(_this.onfocus){
			var data = _this.dataList[tar - 1];
			_this.onfocus(data,tar,_this);
		}
	},
	blurItem:function(event){
		var a = event.currentTarget;
		var par = a.parentNode.parentNode;
		var id = a.id;
		var idArray = id.split('-');
		var _this = TextListMap[idArray[0]];
		var tar = parseInt(idArray[2]);
		
		par.className = _this.normalClass;
		if(_this.onblur){
			var data = _this.dataList[tar - 1];
			_this.onblur(data,tar,_this);
		}
	},
	clickItem:function(event){
		var a = event.currentTarget;
		var id = a.id;
		var idArray = id.split('-');
		var _this = TextListMap[idArray[0]];
		var tar = parseInt(idArray[2]);
		_this.curItemId = tar;
		if(_this.onclick){
			var data = _this.dataList[tar - 1];
			_this.onclick(data,tar,_this);
		}
	},
	appendDiv:function(nId,listBox){
		var totalNum = this.dataList.length;
		var div = document.createElement('div');
		if(nId>totalNum){
			div.setAttribute('class',this.emptyClass);
			div.innerHTML = '&nbsp;';
			listBox.appendChild(div);
		}else{
			div.setAttribute('class',this.normalClass);
			var data = this.dataList[nId - 1];
			div.innerHTML = this.formatHtml(data,nId); 
			listBox.appendChild(div);
			
			var a = document.getElementById(this.target+'-a-'+nId);
			a.onfocus = this.focusItem;
			a.onblur = this.blurItem;
			a.onclick = this.clickItem;
		}
	},
	pageUp:function(justForPageA,tip){
		if(!this.showSplit){return ;} 
		if(this.topItemId > 0){
			var a = document.getElementById(justForPageA);
			a.focus();
			
			var prevPage = this.topItemId - this.disNum;
			var oldCurItemId = this.curItemId;
			if(prevPage < 0 ){
				prevPage = 0;
			}
			
			this.topItemId = prevPage;
			if(prevPage+this.disNum >= oldCurItemId){
				this.curItemId = oldCurItemId;
			}else{
				this.curItemId = prevPage+this.disNum;
			}
			this.refreshContent();
			var na = document.getElementById(this.target+'-a-'+this.curItemId);
			na.focus();
		}else{
			if(tip){
				showGTip(tip,3000);
			}
		}
	},
	pageDown:function(justForPageA,tip){
		if(!this.showSplit){return ;} 
		var dataList = this.dataList;
		var nextPage = this.topItemId + this.disNum;
		if(nextPage <dataList.length){
			var a = document.getElementById(justForPageA);
			a.focus();
			
			this.topItemId = nextPage;
			this.curItemId = nextPage + 1;
			this.refreshContent();
			
			
			var na = document.getElementById(this.target+'-a-'+(nextPage+1));
			na.focus();
		}else{
			showGTip(tip,3000);
		}
	},
	moveItem:function(tar){
		if (tar == this.focusItemId) { this.changeSplit();return ;}
		
		var listBox = document.getElementById(this.content);
		var childNodes = listBox.childNodes;
		var dataList = this.dataList;
		var totalNum = dataList.length;
		
		if (tar > this.focusItemId) { //向下
			this.focusItemId = tar;
			
			var bo = 0;
			if(tar - this.topItemId == this.disNum && tar != totalNum){		//当焦点不是在最上面的时候
				bo = 1;
			}else if(tar == this.topItemId + this.disNum + 1){			//当焦点是第一个时
				bo = 2;
			}
			
			if (bo>0) { //
				
				if( childNodes.length  >= this.disNum + 2 ){
					listBox.removeChild(childNodes[0]);		//删除第一个节点
				}
				
				var dis = 2;
				if(bo==2){dis =1;};
				
				
				var nId = tar+dis;
				this.appendDiv(nId,listBox);
				this.topItemId++;
				this.changeSplit();
			}
		}else if(tar<this.focusItemId){ //向上
			this.focusItemId = tar;
			
			var bo = 0;
			if(tar - this.topItemId == 1 && tar!= 1){		//当焦点不是在最上面的时候
				bo = 1;
			}else if(tar == this.topItemId){			//当焦点是第一个时
				bo = 2;
			}
			
			if(bo>0){	
				if( childNodes.length  >= this.disNum + 2){
					listBox.removeChild(childNodes[childNodes.length - 1]);		//删除最后一个节点
				}
				
				var dis = 2;
				if(bo==2){dis =1;};
				
				var pId = tar-dis;
				var div = document.createElement('div');
				
				if(pId<1){
					div.setAttribute('class',this.emptyClass);
					div.innerHTML = '&nbsp;';
					listBox.insertBefore(div,childNodes[0]);
				}else{
					div.setAttribute('class',this.normalClass);
					var data = this.dataList[pId - 1];
					div.innerHTML = this.formatHtml(data,pId);
					listBox.insertBefore(div,childNodes[0]);
					
					var a = document.getElementById(this.target+'-a-'+pId);
					a.onfocus = this.focusItem;
					a.onblur = this.blurItem;
					a.onclick = this.clickItem;
				}
				
				
				
				this.topItemId--;
				this.changeSplit();
			}
		}
	},
	
}